<?php

namespace plugin\muadmin\app\admin\controller\auth;

use plugin\muadmin\app\common\base\BaseAdminController;
use plugin\muadmin\app\common\model\AuthGroup;
use plugin\muadmin\app\common\model\AuthRule;

class Group extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new AuthGroup();
        // 获取所有父级菜单
        $pidList = $this->model->where('status',1)
            ->order('pid asc')
            ->select()->toArray();
        $pidList = list_to_tree($pidList,'id','pid','children');
        $this->assignConfig('pidList',$pidList);
        if($this->request->action=='edit'){
            $ids = $this->param('ids');
            $menu = $this->auth->getGroups($ids,true,false);
        }else{
            $menu = $this->auth->getAdminMenu(false);
        }
//        print_r($menus);
//        $menu = AuthRule::where('status',1)
//            ->order('id asc,weigh desc')
//            ->select()->toArray();
        foreach ($menu as &$v){
            $v['title'] = __($v['title']);
        }
        unset($v);
        $menu = list_to_tree($menu,'id','pid','children');
        $this->assignConfig('menuList',$menu);
    }
    public function add()
    {
        if($this->request->isAjax()){
            $data = $this->param('row');
            if(!empty($data['rules'])){
                $rules = [];
                foreach ($data['rules'] as $v){
                    if(is_array($v)){
                        $rules = array_merge($rules,$v);
                    }else{
                        $rules[] = $v;
                    }
                }
                $rules = array_filter($rules);
                $rules = array_unique($rules);
                $data['rules'] = implode(',',$rules);
            }
            $res = $this->model->create($data);
            if($res){
                return $this->success("添加成功");
            }else{
                return $this->error("添加失败");
            }
        }
        return parent::add();
    }
    public function edit()
    {
        $ids = $this->param('ids');
        $row = $this->model->where('id',$ids)->find();
        if($this->request->isAjax()){
            $data = $this->param('row');
            if(!empty($data['rules'])){
                $rules = [];
                foreach ($data['rules'] as $v){
                    if(is_array($v)){
                        $rules = array_merge($rules,$v);
                    }else{
                        $rules[] = $v;
                    }
                }
                $rules = array_filter($rules);
                $rules = array_unique($rules);
                $data['rules'] = implode(',',$rules);
            }
            $res = $row->save($data);
            if($res){
                return $this->success("修改成功");
            }
            return $this->error("修改失败");
        }
        $row = $row->toArray();

        foreach ($row['rules'] as $k=>$v){
            $row['rules'][$k] = intval($v);
        }
        $this->assignConfig('row',$row);
        return $this->fetch();
    }
}